<?php

namespace App\Http\Controllers;

// use Illuminate\Http\Request;
// use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use Request;

use App\Models\Book;
use App\Models\SearchJilu;

class BookSearchController extends Controller
{


    public function index()
    {
        // 记录搜索的内容
        $search_text = request::input('search_text');

        if(Session::get('nameid')) {
            $uid = Session::get('nameid');
        }else {
            $uid = 0;
        }

        $search = SearchJilu::where('search_text', $search_text)->where('uid', $uid)->first();

        if($search == null) {
            if(trim($search_text) != '')
            {
                $searchjilu = new SearchJilu;
                $searchjilu->search_text = $search_text;
                $searchjilu->uid = $uid;
                $searchjilu->add_time = time();
                $searchjilu->save();
            }
        } else {
            $search->add_time = time();
            $search->save();
        }

        // 显示搜索出来的图书 每页显示15个记录
        $pre_page = 15;

        //获得所有搜索出来的图书信息的数据->books数据库 按倒序        
        $search_text = urldecode($search_text);
        $books = Book::where('book_name', 'like', '%'.trim($search_text).'%')
                        ->OrWhere('zuozhe', 'like', '%'.trim($search_text).'%')
                        ->OrWhere('isbn', 'like', '%'.trim($search_text).'%')
                        ->orderBy('last_update', 'desc')->orderBy('book_id', 'desc')->paginate($pre_page);

        //return view('books.layouts.search', compact('books'),compact('search_text'));
        return view('books.layouts.search', compact('books', 'search_text'));
        //return view('books.layouts.search', compact('books'))->with('search_text', $search_text);
        //return view('books.layouts.search')->with(compact('books'))->with(compact('search_text'));

    }

    
}
